package org.example.common.pulsar;

import cn.hutool.core.date.DateUtil;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;

/**
 * @author zhangjungang
 * @desc produce
 * @date 2021/12/15 下午4:21
 */
public class TestProduce {

    public static void main(String[] args) throws Exception{

        String serverUrl = "http://127.0.0.1:8080";
        PulsarClient client = PulsarClient.builder().serviceUrl(serverUrl).build();
        Producer<String> producer = client.newProducer(Schema.STRING)
                .blockIfQueueFull(true)
                .create();
        int cnt = 0;
        while (true){
            String line = "cnt="+(++cnt)+" time="+ DateUtil.now();
            producer.newMessage().deliverAfter(10, TimeUnit.SECONDS).value(line).send();
            System.out.println(line);
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        }
    }
}
